﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace GL.Tools
{
    public class EmailUtil
    {
        /// <summary>
        /// 发送方的邮件地址
        /// </summary>
        public string FromAddress { get; set; }

        /// <summary>
        /// 邮件显示的名称
        /// </summary>
        public string DisplayName { get; set; }

        /// <summary>
        /// 接收者的邮箱地址
        /// </summary>
        public string ToAddress { get; set; }

        //抄送
        public string CC { get; set; }

        private string bcc = "";

        public string BCC
        {
            get { return bcc; }
            set { bcc = value; }
        }

        /// <summary>
        /// 邮箱登录名,大部分登录名是邮箱地址,但不是所有的邮箱地址都是登录名,所有这里分开写了    
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 邮箱登录密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 指定的 SMTP 服务器
        /// </summary>
        public string SmtpServer { get; set; }

        private int smtpPort = 25;

        /// <summary>
        /// 端口
        /// </summary>
        public int SmtpPort
        {
            get { return smtpPort; }
            set { smtpPort = value; }
        }
        private bool enableSSL = false;

        public bool EnableSSL
        {
            get { return enableSSL; }
            set { enableSSL = value; }
        }



        /// <summary>
        /// 通过qq邮箱发送邮件发送邮件
        /// </summary>
        /// <param name="qqMail">qq邮箱</param>
        /// <param name="qq">qq号</param>
        /// <param name="password">qq邮箱登录密码</param>
        /// <param name="toAddress">接收者的邮箱地址</param>
        /// <param name="displayName">邮件显示的名称</param>
        /// <param name="title">邮件标题</param>
        /// <param name="body">邮件内容</param>
        public static void SendByQQ(String qqMail, String qq, String password, String toAddress, String displayName, String title, String body)
        {
            Send(qqMail, qq, password, toAddress, displayName, title, body, null, null, true, "smtp.qq.com", 25, false);
        }



        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="fromAddress">发送方的邮件地址</param>
        /// <param name="password">邮箱登录密码</param>
        /// <param name="toAddress">接收者的邮箱地址</param>
        /// <param name="displayName">邮件显示的名称</param>
        /// <param name="title">邮件标题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="smtpHost">邮件提供商主机</param>
        public static void Send(String fromAddress, String password, String toAddress, String displayName, String title, String body, String smtpHost)
        {
            Send(fromAddress, fromAddress, password, toAddress, displayName, title, body, null, null, true, smtpHost, 25, false);
        }
        

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="fromAddress">发送方的邮件地址</param>
        /// <param name="userName">邮箱登录名,大部分登录名是邮箱地址,但不是所有的邮箱地址都是登录名,所有这里分开写了    </param>
        /// <param name="password">邮箱登录密码</param>
        /// <param name="toAddress">接收者的邮箱地址</param>
        /// <param name="displayName">邮件显示的名称</param>
        /// <param name="title">邮件标题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="ccCopy">邮件抄送</param>
        /// <param name="bccCopy"></param>
        /// <param name="isBodyHtml">是否以html格式显示</param>
        /// <param name="smtpHost">邮件提供商主机</param>
        /// <param name="smtpPort">邮件提供商主机端口</param>
        /// <param name="enableSsl">是否加密发送</param>
        public static void Send(String fromAddress, String userName, String password, String toAddress, String displayName, String title, String body, String ccCopy, String bccCopy, bool isBodyHtml, String smtpHost, int smtpPort, bool enableSsl)
        {

            //发送方信息
            MailAddress from = new MailAddress(fromAddress, displayName);

            //接收方地址
            MailAddress to = new MailAddress(toAddress);

            //邮件信息
            MailMessage mailMessage = new MailMessage(from, to);
            mailMessage.Subject = title;
            mailMessage.Body = body;
            mailMessage.IsBodyHtml = true;

            //判断用户是否设置了抄送
            if (!String.IsNullOrEmpty(ccCopy))
            {
                MailAddress CCcopy = new MailAddress(ccCopy);
                mailMessage.CC.Add(CCcopy);
            }
            if (!String.IsNullOrEmpty(bccCopy))
            {
                MailAddress BCCcopy = new MailAddress(bccCopy);
                mailMessage.Bcc.Add(BCCcopy);
            }

            SmtpClient client = new SmtpClient(smtpHost, smtpPort);
            client.EnableSsl = enableSsl;
            client.Credentials = new System.Net.NetworkCredential(userName, password);
            client.Send(mailMessage);
        }


        /// <summary>
        /// 发送邮件(不带附件)
        /// </summary>
        /// <param name="title">邮件标题</param>
        /// <param name="body">邮件内容</param>
        /// <returns></returns>
        public bool SendEmail(string title, string body)
        {
            try
            {
                MailAddress from = new MailAddress(this.FromAddress, this.DisplayName);

                MailAddress to = new MailAddress(this.ToAddress);
                MailMessage mailMessage = new MailMessage(from, to);
                mailMessage.Subject = title;
                mailMessage.Body = body;
                mailMessage.IsBodyHtml = true;
                if (this.CC != string.Empty)
                {
                    MailAddress CCcopy = new MailAddress(CC);
                    mailMessage.CC.Add(CCcopy);
                }

                if (!String.IsNullOrEmpty(this.BCC))
                {
                    MailAddress BCCcopy = new MailAddress(BCC);
                    mailMessage.Bcc.Add(BCCcopy);
                }

                SmtpClient b = new SmtpClient(this.SmtpServer, this.SmtpPort);

                b.EnableSsl = this.EnableSSL;

                b.Credentials = new System.Net.NetworkCredential(this.UserName, this.Password);
                b.Send(mailMessage);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 发送邮件(带附件)
        /// </summary>
        public bool SendEmail(string subject, string body, List<string> sAttrachment)
        {
            try
            {
                MailMessage mailMessage = new MailMessage(FromAddress, ToAddress);
                mailMessage.Subject = subject;
                mailMessage.Body = body;
                mailMessage.IsBodyHtml = true;
                if (CC != string.Empty)
                {
                    MailAddress CCcopy = new MailAddress(CC);
                    mailMessage.CC.Add(CCcopy);
                }

                if (BCC != string.Empty)
                {
                    MailAddress BCCcopy = new MailAddress(BCC);
                    mailMessage.Bcc.Add(BCCcopy);
                }

                if (sAttrachment.Count > 0)
                {
                    foreach (string sSubstr in sAttrachment)
                    {
                        System.Net.Mail.Attachment MyAttachment = new Attachment(sSubstr);
                        mailMessage.Attachments.Add(MyAttachment);
                    }
                }


                SmtpClient b = new SmtpClient(SmtpServer, SmtpPort);
                b.EnableSsl = EnableSSL;
                b.Credentials = new System.Net.NetworkCredential(UserName, Password);
                b.Send(mailMessage);
                return true;
            }
            catch
            {
                return false;
            }

        }
    }
}
